( function($) {

	$.fn.musicPlayer = function(playlist,options){
		return this.each( function(){		
				var defaultOptions = {
				loop: false,
				autoplay: false,
				volume: 0.5,
				manualSeek: false,
				volumeSeek: false,
				loaded: false,
				errorMsg: "Sorry but the song could not be loaded"
				};
				
				var realOptions = $.extend(defaultOptions,options);
				
				/* Our library is going to create the following HTML code
				<audio>
				<src url="http://"> </src>
				</audio>
		
				However, if the browser doesnt support the audio tag, 
				we will fall back to a flash player
				*/
				if ( Modernizr.audio == true){
				
				var audioElem = document.createElement ( "audio" );
				$( audioElem ).appendTo( this );
				$( audioElem ).addClass( "musicPlayer-audio" );
				$( audioElem ).attr("loop",realOptions.loop);
				$( audioElem ).attr("autoplay",realOptions.autoplay);
				$( audioElem ).attr("volume",realOptions.volume);
				
					for(p in playlist)
					{
						var src = document.createElement ( "source" );
						$( src ).appendTo( audioElem );
						$( src ).attr("src",playlist[p]);
						
					}
				//Declarations for the player itself.
				var play = document.createElement ( "span" );
				$( play ).attr ("id","playtoggle");
				$( play ).appendTo ( this );
				var previous = document.createElement ( "span" );
				$( previous ).attr ("id","previous");
				$( previous ).appendTo ( this );
				var next = document.createElement ( "span" );
				$( next ).attr ("id","nextSong");
				$( next ).appendTo ( this );
				var gutter = document.createElement ( "span" );
				$( gutter ).attr("id","gutter");
				$( gutter ).appendTo ( this );
				var handle = document.createElement ( "span" );
				$( handle ).attr("id","handle");
				$( handle ).addClass("ui-slider-handle");
				$( handle ).appendTo( gutter );
				var loading = document.createElement ( "span" );
				$( loading ).attr("id","loading");
				$( loading ).appendTo( gutter );
				var timeleft = document.createElement ( "span" );
				$( timeleft ).attr("id","timeleft");
				$( timeleft ).appendTo( this );
				var volume = document.createElement ("span");
				$ ( volume ).attr("id","volume");
				$ ( volume ).appendTo ( this );
				var volumeSlider = document.createElement ( "span" );
				$( volumeSlider ).attr("id","volumeSlider");
				$( volumeSlider ).appendTo ( this );
				var volumeHandle = document.createElement ( "span" );
				$( volumeHandle ).attr("id","volumeHandle");
				$( volumeHandle ).addClass("ui-slider-handle");
				$( volumeHandle ).appendTo( volumeSlider );
				
				
				
				audio = $('#thePlayer audio').get(0);
				audio.addEventListener('ended',nextSong,false);
				audio.addEventListener('loadeddata',setVar,false);
				loadingIndicator = $('#thePlayer #loading');
				positionIndicator = $('#thePlayer #handle');
				timeleft = $('#thePlayer #timeleft');
    
				if ((audio.buffered != undefined) && (audio.buffered.length != 0)) {
				$(audio).bind('progress', function() {
				var loaded = parseInt(((audio.buffered.end(0) / audio.duration) * 100), 10);
				loadingIndicator.css({width: loaded + '%'});
				});
				}
				else {
				loadingIndicator.remove();
				}
				
				$(audioElem).bind('play',function() {
				$("#playtoggle").addClass('playing');   
				}).bind('pause ended', function() {
				$("#playtoggle").removeClass('playing');    
				}); 
				
				$("#playtoggle").click(function() {     
				if (audio.paused) { audio.play(); } 
				else { audio.pause(); }     
				});
				
				$('#nextSong').click(function() {
				var song = $('#thePlayer audio source').next().get(0);
				audio.pause();
				console.log(audio.duration);
				audio.src=song.src;
				audio.load();
				audio.play();
				});
				
				$('#previous').click(function() {
				var song = $('#thePlayer audio source').prev().get(0);
				audio.pause();
				audio.src=song.src;
				audio.load();
				audio.play();
				});		
				
				
				$(audio).bind('timeupdate', function() {
    
				var rem = parseInt(audio.currentTime),
				pos = (audio.currentTime / audio.duration) * 100,
				mins = Math.floor(rem/60,10),
				secs = rem - mins*60;
				var duration = parseInt(audio.duration);
				var durationmins = Math.floor(duration/60,10);
				var durationsecs = duration - durationmins*60;
				
				timeleft.text(mins + ':' + (secs > 9 ? secs : '0' + secs) + '/' + durationmins + ":" + durationsecs);
				if (!realOptions.manualSeek) { positionIndicator.css({left: pos + '%'}); }
				if (!realOptions.loaded) {
				realOptions.loaded = true;
				
				$('#thePlayer #gutter').slider({
				  value: 0,
				  step: 0.01,
				  orientation: "horizontal",
				  range: "min",
				  max: audio.duration,
				  animate: true,          
				  slide: function() {             
					realOptions.manualSeek = true;
				  },
				  stop:function(e,ui) {
					realOptions.manualSeek = false;
					audio.currentTime = ui.value;
					console.log(ui.value);
				  }
					});
					
				
					$('#volumeSlider').slider({
					orientation: "horizontal",
					value: 0,
					min: 0,
					max: 1,
					range: "min",
					animate: true,
					step: 0.01,
					slide: function(e,ui) {
						audio.volume = ui.value;
						$('#volumeHandle').css({left: ui.value*100+ '%'});
		
						if(ui.value > 0) realOptions.muted = false;
					},
					stop: function(e, ui) { 
					
						audio.volume = ui.value;
					
						$('#volumeHandle').css({left: ui.value*100 + '%'});
						
						if(ui.value > 0) audio.muted = false;
					}	
					});	
					
					$('#volumeHandle').css({left: audio.volume*100+ '%'});
				}
				});
							
				
					
									
				
				function nextSong(){
				var song = $('#thePlayer audio source').next().get(0);
				audio.pause();
				audio.src=song.src;
				audio.load();
							}
					
						var volume = 0;
				$('#volume').click(function() {
					if( audio.muted == false)
					{
					volume = audio.volume;
					audio.muted =!audio.muted;
					$('#volumeHandle').css({left: 0 + '%'});
					}
					else{
					audio.volume = volume;
					$('#volumeHandle').css({left: audio.volume*100 + '%'});
					audio.muted = !audio.muted;
					}
				});
				function setVar(){
				
				audio.play();
				$( "#thePlayer #gutter" ).slider( "option", "max", audio.duration );
				console.log(audio.duration);
				}
				}
				else{
				 /*<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="165" height="37" id="niftyPlayer1" align="">
		 <param name=movie value="niftyplayer/niftyplayer.swf?file=betty.mp3&as=0">
		 <param name=quality value=high>
		 <param name=bgcolor value=#FFFFFF>
		 <embed src="niftyplayer/niftyplayer.swf?file=betty.mp3&as=0" quality=high bgcolor=#FFFFFF width="165" height="37" name="niftyPlayer1" align="" type="application/x-shockwave-flash" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer">
		</embed>
		</object>*/
				
				var object = document.createElement ( "object" );
				$( object ).attr ("classid","clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
				$( object ).attr ("codebase","http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0");
				$( object ).attr ("width", "165");	
				$( object ).attr ("height", "37");
				$( object ).attr ("id", "FMrPlayer");
				$( object ).appendTo ( this );
				var param1 = document.createElement ("param");
				$( param1 ).attr("name","movie");
				$( param1 ).attr("value","niftyplayer/niftyplayer.swf?file=http://jeffrey-way.s3.amazonaws.com/zelda.mp3&as=0");
				$( param1 ).appendTo('#FMrPlayer');
				var param2 = document.createElement ("param");
				$( param2 ).attr("name","quality");
				$( param2 ).attr("value","high");
				$( param2 ).appendTo('#FMrPlayer');
				var param3 = document.createElement ("param");
				$( param3 ).attr("name","bgcolor");
				$( param3 ).attr("value","#FFFFFF");
				$( param3 ).appendTo('#FMrPlayer');
				var embed = document.createElement ("embed");
				$( embed ).attr("src","niftyplayer/niftyplayer.swf?file=http://jeffrey-way.s3.amazonaws.com/zelda.mp3&as=0");
				$( embed ).attr("quality","high");
				$( embed ).attr("bgcolor","#FFFFFF");
				$( embed ).attr("width","165");
				$( embed ).attr("height","37");
				$( embed ).attr("quality","high");
				$( embed ).attr("name","FMrPlayer");
				$( embed ).attr("align","");
				$( embed ).attr("type","application/x-shockwave-flash");
				$( embed ).attr("swLiveConnect","true");
				$( embed ).attr("pluginspage","http://www.macromedia.com/go/getflashplayer");
				$( embed ).appendTo('#FMrPlayer');
				/*<div id="FMrControls">
			<a href="javascript:FMr.prev()">prev</a>
			<a href="javascript:FMr.next()">next</a>
		</div>
	<ol id="FMrPlaylist">
		<li><a href="http://www.varal.org/media/niftyplayer/creeeeak.mp3" class="playing"></a></li></ol>*/
				var control = document.createElement ("div");
				$( control ).attr("id","FMrControls");
				$( control ).appendTo(this);
				var prev = document.createElement("a");
				$( prev ).attr("href","javascript:FMr.prev()");
				$( prev ).attr("innerText","prev");
				$( prev ).appendTo('#FMrControls');
				var next = document.createElement("a");
				$( next ).attr("href","javascript:FMr.next()");
				$( next ).attr("innerText","next");
				$( next ).appendTo('#FMrControls');
				var ol = document.createElement("ol");
				$( ol ).attr("id","FMrPlaylist");
				$( ol ).appendTo(this);

				for(p in playlist){
				var li = document.createElement("li");
				$( li ).appendTo("#FMrPlaylist");
				$( li ).attr("id",p);
				var play = document.createElement ("a");
				$( play ).attr("href",playlist[p]);
				$( play ).attr("innerText",playlist[p]);
				var id = "#"+p;
				$( play ).appendTo(id);
				}
				FMr.playlist = playlist;
				$( '#thePlayer' ).css('background','white');
				$( '#thePlayer' ).css('border-top','none');
				$( '#thePlayer' ).css('border-bottom','none');
	 }});
	//}
	
}})(jQuery);